package com.yuhs.zixing.study.design.pattern.dp002.structural.s004.composite.c001;

import java.util.ArrayList;

/**
 * @description:
 * @author: yuhs
 * @date: 2022/5/1 9:51 PM
 */
public class Client001 {
    public static void main(String[] args) {
        //首先产生了一个根节点
        IRoot001 ceo = new Root001("王大麻子", "总经理", 100000);
        //产生三个部门经理，也就是树枝节点
        IBranch001 developDep = new Branch001("刘大瘸子", "研发部门经理", 10000);
        IBranch001 salesDep = new Branch001("马二拐子", "销售部门经理", 20000);
        IBranch001 financeDep = new Branch001("赵三驼子", "财务部经理", 30000);
        //再把三个小组长产生出来
        IBranch001 firstDevGroup = new Branch001("杨三乜斜", "开发一组组长", 5000);
        IBranch001 secondDevGroup = new Branch001("吴大棒槌", "开发二组组长", 6000);
        //剩下的就是我们这些小兵了,就是路人甲、路人乙
        ILeaf001 a = new Leaf001("a", "开发人员", 2000);
        ILeaf001 b = new Leaf001("b", "开发人员", 2000);
        ILeaf001 c = new Leaf001("c", "开发人员", 2000);
        ILeaf001 d = new Leaf001("d", "开发人员", 2000);
        ILeaf001 e = new Leaf001("e", "开发人员", 2000);
        ILeaf001 f = new Leaf001("f", "开发人员", 2000);
        ILeaf001 g = new Leaf001("g", "开发人员", 2000);
        ILeaf001 h = new Leaf001("h", "销售人员", 5000);
        ILeaf001 i = new Leaf001("i", "销售人员", 4000);
        ILeaf001 j = new Leaf001("j", "财务人员", 5000);
        ILeaf001 k = new Leaf001("k", "CEO秘书", 8000);
        ILeaf001 zhengLaoLiu = new Leaf001("郑老六", "研发部副总", 20000);
        //该产生的人都产生出来了，然后我们怎么组装这棵树
        //首先是定义总经理下有三个部门经理
        ceo.add(developDep);
        ceo.add(salesDep);
        ceo.add(financeDep);
        //总经理下还有一个秘书
        ceo.add(k);
        //定义研发部门下的结构
        developDep.add(firstDevGroup);
        developDep.add(secondDevGroup);
        //研发部经理下还有一个副总
        developDep.add(zhengLaoLiu);
        //看看开发两个开发小组下有什么
        firstDevGroup.add(a);
        firstDevGroup.add(b);
        firstDevGroup.add(c);
        secondDevGroup.add(d);
        secondDevGroup.add(e);
        secondDevGroup.add(f);
        //再看销售部下的人员情况
        salesDep.add(h);
        salesDep.add(i);
        //最后一个财务
        financeDep.add(j);
        //打印写完的树状结构
        System.out.println(ceo.getInfo());
        //打印出来整个树形
        getAllSubordinateInfo(ceo.getSubordinateInfo());
    }

    //遍历所有的树枝节点，打印出信息
    private static void getAllSubordinateInfo(ArrayList subordinateList) {
        int length = subordinateList.size();
        //定义一个ArrayList长度，不要在for循环中每次计算 
        for (int m = 0; m < length; m++) {
            Object s = subordinateList.get(m);
            if (s instanceof Leaf001) {  //是个叶子节点，也就是员工
                ILeaf001 employee = (ILeaf001) s;
                System.out.println(((Leaf001) s).getInfo());
            } else {
                IBranch001 branch = (IBranch001) s;
                System.out.println(branch.getInfo());
                //再递归调用
                getAllSubordinateInfo(branch.getSubordinateInfo());
            }
        }
    }
}
